package com.sinbad.produce.controller;


import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 消息发送者
 */


@RestController
@RequestMapping("/rabbit/send")
public class SendMqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * direct模式测试
     */
    @GetMapping("/directSend")
    public void directSend(String str) {
        System.out.println("directSend开始发送消息 : " + str);
        rabbitTemplate.convertAndSend("directExchange", "directKey", str);
    }


    /**
     * Fanout Exchange模式
     */
    @GetMapping("fanoutSend")
    public void fanoutSend(String str) {
        /**
         * Tips:由于Fanout不需要绑定RoutingKey因此可以为空
         */
        System.out.println("fanoutSend开始发送消息 : " + str);
        rabbitTemplate.convertAndSend("fanoutExchange", "", str);
    }


    /**
     * Topic转发模式
     */
    @GetMapping("topicSend")
    public void topicSend(String str) {
        System.out.println("topicSend开始发送消息 : " + str);
        rabbitTemplate.convertAndSend("topicExchange", "topic.message.test", str);
        rabbitTemplate.convertAndSend("topicExchange", "rabbitMQ.test.topic", str);

        //下面发送的两条消息不会被消费，因为规则不匹配
        rabbitTemplate.convertAndSend("topicExchange", "rabbitMQ.test.test", str);
        rabbitTemplate.convertAndSend("topicExchange", "test.rabbitMQ.test", str);
    }
}
